package com.situ.ftbserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.situ.ftbserver.mapper.CourseMapper;
import com.situ.ftbserver.mapper.ReserveMapper;
import com.situ.ftbserver.model.Course;
import com.situ.ftbserver.model.Reserve;
import com.situ.ftbserver.model.search.ReserveSearchBean;
import com.situ.ftbserver.service.ReserveService;
import com.situ.ftbserver.util.PaginateInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CacheConfig(cacheNames = "reserveServiceImpl")
@Service
public class ReserveServiceImpl implements ReserveService {
    private ReserveMapper reserveMapper;
    private CourseMapper courseMapper;
    @Autowired
    public void setReserveMapper(ReserveMapper reserveMapper) {
        this.reserveMapper = reserveMapper;
    }
    @Autowired
    public void setCourseMapper(CourseMapper courseMapper) {
        this.courseMapper = courseMapper;
    }

    @Override
    public List<Reserve> findByCourseId(Integer[] ids) {
        QueryWrapper<Reserve> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("course_id", ids);
        List<Reserve> reserves = reserveMapper.selectList(queryWrapper);
        return reserves;
    }

    @Override
    public List<Reserve> findByMemberId(Integer[] ids) {
        QueryWrapper<Reserve> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("member_id", ids);
        return reserveMapper.selectList(queryWrapper);
    }

    @Override
    public Reserve findByCourseAndMember(Integer courseId, Integer memberId) {
        return reserveMapper.selectOne(new QueryWrapper<Reserve>().eq("course_id", courseId).eq("member_id", memberId));
    }


    @Override
    public List<Reserve> findAll(PaginateInfo pi, ReserveSearchBean rsb) {
        try(Page<?> p = PageHelper.startPage(pi.getPageNo(), pi.getPageSize())){
            return reserveMapper.findAll(rsb);
        }

    }

    @CacheEvict(allEntries = true)
    @Override
    public int delete(Integer[] ids) {
        return reserveMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean save(Reserve reserve) {
        return reserveMapper.insert(reserve)>0;
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean update(Reserve reserve) {
        return reserveMapper.updateById(reserve)>0;
    }

    @Cacheable(key = "'getCourseReserveCount'")
    @Override
    public List<Map<String, Integer>> getCourseReserveCount() {
         return reserveMapper.getCourseReserveCount();
    }

    @Override
    public Reserve findById(int id) {
        return reserveMapper.selectById(id);
    }
}
